using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Barrage.Global;
using Spacedash.Logic.Scenario.Cells.Actions;

namespace Spacedash.Logic.Scenario.Cells
{
    /// <summary>
    /// CellManager loads a list of patterns (Name - CellData associations) of a XML file.
    /// This data is used later by the ScenarioLoader to place each CellData in cells of the scenario
    /// </summary>
    public static class CellManager
    {
        const string XML_PATTERNS_ROOT = "patterns";
        const string XML_MESH_ELEMENT = "mesh";
        const string XML_FILE_ATTRIBUTE = "file";
        const string XML_SHADER_ATTRIBUTE = "shader";
        const string XML_COLLISION_ELEMENT = "collision";

        private static Dictionary<string, CellData> cellData = new Dictionary<string, CellData>();
        /// <summary>
        /// Set of CellData instances
        /// </summary>
        public static Dictionary<string, CellData> CellData { get { return cellData; } }

        /// <summary>
        /// Clears the loaded cellDatas
        /// </summary>
        public static void ClearPatterns()
        {
            cellData.Clear();
        }

        /// <summary>
        /// Loads patterns from the specified file, adding them to the list
        /// </summary>
        /// <param name="file">XML file</param>
        public static void LoadPatterns(string file)
        {
            XmlDocument xd = new XmlDocument();
            try
            {
                xd.Load(file);
                foreach(XmlNode xn in xd.ChildNodes)
                {
                    if (xn.Name == XML_PATTERNS_ROOT)
                    {
                        loadPatternData(xn);
                    }
                }
            }
            catch (Exception e)
            {
                throw new System.Exception("Error parsing patterns " + file + ":\n" + e.Message, e);
            }
        }

        static void loadPatternData(XmlNode xn)
        {
            foreach (XmlNode xno in xn.ChildNodes)
            {
                if (xno.NodeType == XmlNodeType.Element)
                    loadPattern(xno);
            }
        }
        static void loadPattern(XmlNode xn)
        {
            CellData data = new CellData();
            data.Name = xn.Name;
            data.Shader = XmlHelper.GetXMLAttribute(xn, XML_SHADER_ATTRIBUTE);
            foreach (XmlNode xno in xn.ChildNodes)
            {
                switch (xno.Name)
                {
                    case XML_MESH_ELEMENT:
                        data.AddMesh(XmlHelper.GetXMLAttribute(xno, XML_FILE_ATTRIBUTE));
                        break;
                    case XML_COLLISION_ELEMENT:
                        data.AddCollisionActions(loadActions(xno.ChildNodes));
                        break;
                }
                
            }
            cellData.Add(data.Name, data);
        }

        static List<CellAction> loadActions(XmlNodeList nodes){
            List<CellAction> actions=new List<CellAction>();
            foreach (XmlNode n in nodes)
            {
                CellAction action = CellAction.loadAction(n);
                if (action!=null)
                    actions.Add(action);
            }
            return actions;
        }
    }
}
